package com.bbzn.device.client.controller;

import com.alibaba.fastjson.JSONObject;
import com.bbzn.device.client.vo.req.DeviceVO;
import com.bbzn.device.client.aspect.OperationAnnotation;
import com.bbzn.device.client.config.EmqCode;
import com.bbzn.device.client.constant.CrudConstant;
import com.bbzn.device.client.constant.DeviceConstant;
import com.bbzn.device.client.dataobject.Device;
import com.bbzn.device.client.dataobject.SystemParam;
import com.bbzn.device.client.dto.device.*;
import com.bbzn.device.client.exception.DefinitException;
import com.bbzn.device.client.service.CommonService;
import com.bbzn.device.client.service.DeviceService;
import com.bbzn.device.client.service.PublishService;
import com.bbzn.device.client.service.SystemParamByCacheService;
import com.bbzn.device.client.utils.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * @Author 蒋洪媛
 * @Date 2021/10/30 15:20
 * @Version 2.0
 */

@RestController
@RequestMapping("/deviceinfo")
@Slf4j
//@CrossOrigin
public class DeviceInfoController extends BaseApiService {


    @Autowired
    private CommonService commonService;

    @Autowired
    private SystemParamByCacheService systemParamByCacheService;

    @Autowired
    private DeviceService deviceService;

    @Autowired
    private PublishService publishService;

    @Autowired
    private RedisUtils redisUtils;


    /**
     * 查询设备详情
     *
     * @param reqVO
     * @param request
     * @return
     * @throws Exception
     */
    @OperationAnnotation(content = "查询设备详情")
    @PostMapping(value = "/getDeviceInfo")
    public BaseResponse getDeviceInfo(@RequestBody DeviceVO reqVO, HttpServletRequest request) throws Exception {
        SystemParam systemParam = null;
        String secretKey = commonService.getWebSalt(request);
        String language = MayiktJwtUtils.getLanguage(request, secretKey);
        Long userId = commonService.getUserId(request);
        if (!OptionalBean.ofNullable(reqVO.getImei()).isPresent()) {
            systemParam = systemParamByCacheService.getValues(DeviceConstant.DEVICE_ENUM, DeviceConstant.DEVICE_NO_IMEI_PREFIX + language);
            throw new DefinitException(systemParam.getCode(), systemParam.getMassage());
        }

        DeviceInfoDTO deviceInfoDTO = deviceService.selectByImei(reqVO.getImei());
        return setResultSuccess(deviceInfoDTO);

    }



    /**
     * 设置心率预警阈值
     * <p>
     * 产品经理要求此处数据强一致性，而非最终一致性
     * 实时数据需要等待设备响应
     * 1.修改数据库状态
     * 2.发送主题
     * 3.数据校验
     *
     * @param reqVO
     * @param request
     * @return
     * @throws Exception
     */
    @OperationAnnotation(content = "设置心率预警阈值")
    @PostMapping(value = "/setHeartRate")
    public BaseResponse setHeartRate(@RequestBody DeviceVO reqVO, HttpServletRequest request) throws Exception {
        SystemParam systemParam = null;
        String secretKey = commonService.getWebSalt(request);
        String language = MayiktJwtUtils.getLanguage(request, secretKey);
        Long userId = commonService.getUserId(request);
        if (!OptionalBean.ofNullable(reqVO.getImei()).isPresent()) {
            systemParam = systemParamByCacheService.getValues(DeviceConstant.DEVICE_ENUM, DeviceConstant.DEVICE_NO_IMEI_PREFIX + language);
            throw new DefinitException(systemParam.getCode(), systemParam.getMassage());
        }
        if (!OptionalBean.ofNullable(reqVO.getHeartUpper()).isPresent()) {
            systemParam = systemParamByCacheService.getValues(DeviceConstant.DEVICE_ENUM, DeviceConstant.DEVICE_NU_HEARTUPPER_PREFIX + language);
            throw new DefinitException(systemParam.getCode(), systemParam.getMassage());
        }
        if (!OptionalBean.ofNullable(reqVO.getHeartLower()).isPresent()) {
            systemParam = systemParamByCacheService.getValues(DeviceConstant.DEVICE_ENUM, DeviceConstant.DEVICE_NU_HEARTLOWER_PREFIX + language);
            throw new DefinitException(systemParam.getCode(), systemParam.getMassage());
        }
        if (!OptionalBean.ofNullable(reqVO.getRateInterval()).isPresent()) {
            systemParam = systemParamByCacheService.getValues(DeviceConstant.DEVICE_ENUM, DeviceConstant.DEVICE_UN_RATE_INTERVAL_PREFIX + language);
            throw new DefinitException(systemParam.getCode(), systemParam.getMassage());
        }

        Device device = new Device();
        // 心率设置状态【0：修改成功 / 1：修改中】
        device.setRateState(1);
        device.setImei(reqVO.getImei());
        device.setHeartLower(reqVO.getHeartLower());
        device.setHeartUpper(reqVO.getHeartUpper());
        device.setRateInterval(reqVO.getRateInterval());
        // 1.修改心率预警阈值
        deviceService.updateByImeiSelective(device);

        // 2.发送消息 【设置心率预警阈值】

        // 组装json
        JSONObject data = new JSONObject();
        data.put("code", EmqCode.rateCode);
        data.put("imei", reqVO.getImei());
        data.put("heartUpper", reqVO.getHeartUpper());
        data.put("heartLower", reqVO.getHeartLower());
        data.put("interval", reqVO.getRateInterval());
        // 推送设置心率预警阈值topic给设备
        publishService.sendToMqtt(data.toJSONString(), EmqCode.sendTopic + reqVO.getImei() + EmqCode.endTopic);
        systemParam = systemParamByCacheService.getValues(CrudConstant.CRUD_ENUM, CrudConstant.MODIFY_SUCCESS_PREFIX + language);
        return setResultSuccess(systemParam.getMassage());
    }



    /**
     * 设置血氧预警阈值
     * <p>
     * 产品经理要求此处数据强一致性，而非最终一致性
     * 实时数据需要等待设备响应
     * 1.修改数据库状态
     * 2.发送主题
     * 3.数据校验
     *
     * @param reqVO
     * @param request
     * @return
     * @throws Exception
     */
    @OperationAnnotation(content = "设置血氧预警阈值")
    @PostMapping(value = "/setBloodOxy")
    public BaseResponse setBloodOxy(@RequestBody DeviceVO reqVO, HttpServletRequest request) throws Exception {
        SystemParam systemParam = null;
        String secretKey = commonService.getWebSalt(request);
        String language = MayiktJwtUtils.getLanguage(request, secretKey);
        Long userId = commonService.getUserId(request);
        if (!OptionalBean.ofNullable(reqVO.getImei()).isPresent()) {
            systemParam = systemParamByCacheService.getValues(DeviceConstant.DEVICE_ENUM, DeviceConstant.DEVICE_NO_IMEI_PREFIX + language);
            throw new DefinitException(systemParam.getCode(), systemParam.getMassage());
        }
        if (!OptionalBean.ofNullable(reqVO.getOxyUpper()).isPresent()) {
            systemParam = systemParamByCacheService.getValues(DeviceConstant.DEVICE_ENUM, DeviceConstant.DEVICE_NU_OXYUPPER_PREFIX + language);
            throw new DefinitException(systemParam.getCode(), systemParam.getMassage());
        }
        if (!OptionalBean.ofNullable(reqVO.getOxyLower()).isPresent()) {
            systemParam = systemParamByCacheService.getValues(DeviceConstant.DEVICE_ENUM, DeviceConstant.DEVICE_NU_OXYLOWER_PREFIX + language);
            throw new DefinitException(systemParam.getCode(), systemParam.getMassage());
        }
        if (!OptionalBean.ofNullable(reqVO.getOxyInterval()).isPresent()) {
            systemParam = systemParamByCacheService.getValues(DeviceConstant.DEVICE_ENUM, DeviceConstant.DEVICE_UN_BLOODOXY_INTERVAL_PREFIX + language);
            throw new DefinitException(systemParam.getCode(), systemParam.getMassage());
        }

        Device device = new Device();
        // 血氧设置状态【0：修改成功 / 1：修改中】
        device.setOxyState(1);
        device.setImei(reqVO.getImei());
        device.setOxyUpper(reqVO.getOxyUpper());
        device.setOxyLower(reqVO.getOxyLower());
        device.setOxyInterval(reqVO.getOxyInterval());
        // 1.修改血氧预警阈值
        deviceService.updateByImeiSelective(device);

        // 2.发送消息 【设置血氧预警阈值】

        // 组装json
        JSONObject data = new JSONObject();
        data.put("code", EmqCode.oxyCode);
        data.put("imei", reqVO.getImei());
        data.put("OxyUpper", reqVO.getOxyUpper());
        data.put("OxyLower", reqVO.getOxyLower());
        data.put("interval", reqVO.getOxyInterval());
        // 推送设置血氧预警阈值topic给设备
        publishService.sendToMqtt(data.toJSONString(), EmqCode.sendTopic + reqVO.getImei() + EmqCode.endTopic);
        systemParam = systemParamByCacheService.getValues(CrudConstant.CRUD_ENUM, CrudConstant.MODIFY_SUCCESS_PREFIX + language);
        return setResultSuccess(systemParam.getMassage());
    }


    /**
     * 查询心率预警阈值和心率上报时间间隔
     *
     * @param reqVO
     * @param request
     * @return
     * @throws Exception
     */
    @OperationAnnotation(content = "查询心率预警阈值和心率上报时间间隔")
    @PostMapping(value = "/getHeartRate")
    public BaseResponse getHeartRate(@RequestBody DeviceVO reqVO, HttpServletRequest request) throws Exception {
        SystemParam systemParam = null;
        String secretKey = commonService.getWebSalt(request);
        String language = MayiktJwtUtils.getLanguage(request, secretKey);
        Long userId = commonService.getUserId(request);
        if (!OptionalBean.ofNullable(reqVO.getImei()).isPresent()) {
            systemParam = systemParamByCacheService.getValues(DeviceConstant.DEVICE_ENUM, DeviceConstant.DEVICE_NO_IMEI_PREFIX + language);
            throw new DefinitException(systemParam.getCode(), systemParam.getMassage());
        }
        RateDTO rateDTO = deviceService.getRateInfoByImei(reqVO.getImei());
        return setResultSuccess(rateDTO);
    }


    /**
     * 查询血氧预警阈值和血氧上报时间间隔
     *
     * @param reqVO
     * @param request
     * @return
     * @throws Exception
     */
    @OperationAnnotation(content = "查询血氧预警阈值和血氧上报时间间隔")
    @PostMapping(value = "/getBloodOxy")
    public BaseResponse getBloodOxy(@RequestBody DeviceVO reqVO, HttpServletRequest request) throws Exception {
        SystemParam systemParam = null;
        String secretKey = commonService.getWebSalt(request);
        String language = MayiktJwtUtils.getLanguage(request, secretKey);
        Long userId = commonService.getUserId(request);
        if (!OptionalBean.ofNullable(reqVO.getImei()).isPresent()) {
            systemParam = systemParamByCacheService.getValues(DeviceConstant.DEVICE_ENUM, DeviceConstant.DEVICE_NO_IMEI_PREFIX + language);
            throw new DefinitException(systemParam.getCode(), systemParam.getMassage());
        }
        BloodOxyDTO bloodOxyDTO = deviceService.getBloodOxyInfoByImei(reqVO.getImei());
        return setResultSuccess(bloodOxyDTO);
    }


    /**
     * 设置体温预警阈值
     * 产品经理要求此处数据强一致性，而非最终一致性
     * 实时数据需要等待设备响应
     * 1.修改数据库
     * 2.发送主题
     * 3.数据校验
     *
     * @param reqVO
     * @param request
     * @return
     * @throws Exception
     */
    @OperationAnnotation(content = "设置体温预警阈值")
    @PostMapping(value = "/setTemp")
    public BaseResponse setTemp(@RequestBody DeviceVO reqVO, HttpServletRequest request) throws Exception {
        SystemParam systemParam = null;
        String secretKey = commonService.getWebSalt(request);
        String language = MayiktJwtUtils.getLanguage(request, secretKey);
        Long userId = commonService.getUserId(request);
        if (!OptionalBean.ofNullable(reqVO.getImei()).isPresent()) {
            systemParam = systemParamByCacheService.getValues(DeviceConstant.DEVICE_ENUM, DeviceConstant.DEVICE_NO_IMEI_PREFIX + language);
            throw new DefinitException(systemParam.getCode(), systemParam.getMassage());
        }
        if (!OptionalBean.ofNullable(reqVO.getTempUpper()).isPresent()) {
            systemParam = systemParamByCacheService.getValues(DeviceConstant.DEVICE_ENUM, DeviceConstant.DEVICE_NU_TEMPERATUREUPPER_PREFIX + language);
            throw new DefinitException(systemParam.getCode(), systemParam.getMassage());
        }
        if (!OptionalBean.ofNullable(reqVO.getTempLower()).isPresent()) {
            systemParam = systemParamByCacheService.getValues(DeviceConstant.DEVICE_ENUM, DeviceConstant.DEVICE_NU_TEMPERATURELOWER_PREFIX + language);
            throw new DefinitException(systemParam.getCode(), systemParam.getMassage());
        }
        if (!OptionalBean.ofNullable(reqVO.getTempInterval()).isPresent()) {
            systemParam = systemParamByCacheService.getValues(DeviceConstant.DEVICE_ENUM, DeviceConstant.DEVICE_UN_TEMP_INTERVAL_PREFIX + language);
            throw new DefinitException(systemParam.getCode(), systemParam.getMassage());
        }

        Device device = new Device();
        device.setImei(reqVO.getImei());
        device.setTempLower(reqVO.getTempLower());
        device.setTempUpper(reqVO.getTempUpper());
        device.setTempInterval(reqVO.getTempInterval());
        // 体温设置状态【0：修改成功 / 1：修改中】
        device.setTempState(1);
        // 1.修改体温预警阈值
        deviceService.updateByImeiSelective(device);


        // 2.发送消息 【设置体温预警阈值】
        // 组装json
        JSONObject data = new JSONObject();
        data.put("code", EmqCode.temperateCode);
        data.put("imei", reqVO.getImei());
        data.put("temperatureUpper", reqVO.getTempUpper());
        data.put("temperatureLower", reqVO.getTempLower());
        data.put("interval", reqVO.getTempInterval());
        // 推送设置体温预警阈值topic给设备
        publishService.sendToMqtt(data.toJSONString(), EmqCode.sendTopic + reqVO.getImei() + EmqCode.endTopic);
        systemParam = systemParamByCacheService.getValues(CrudConstant.CRUD_ENUM, CrudConstant.MODIFY_SUCCESS_PREFIX + language);
        return setResultSuccess(systemParam.getMassage());
    }


    /**
     * 查询体温预警阈值和体温上报时间间隔
     *
     * @param reqVO
     * @param request
     * @return
     * @throws Exception
     */
    @OperationAnnotation(content = "查询体温预警阈值和体温上报时间间隔")
    @PostMapping(value = "/getTemp")
    public BaseResponse getTemp(@RequestBody DeviceVO reqVO, HttpServletRequest request) throws Exception {
        SystemParam systemParam = null;
        String secretKey = commonService.getWebSalt(request);
        String language = MayiktJwtUtils.getLanguage(request, secretKey);
        if (!OptionalBean.ofNullable(reqVO.getImei()).isPresent()) {
            systemParam = systemParamByCacheService.getValues(DeviceConstant.DEVICE_ENUM, DeviceConstant.DEVICE_NO_IMEI_PREFIX + language);
            throw new DefinitException(systemParam.getCode(), systemParam.getMassage());
        }
        TempDTO tempDTO = deviceService.getTempInfoByImei(reqVO.getImei());
        return setResultSuccess(tempDTO);
    }

    /**
     * 设置血压预警阈值
     * 产品经理要求此处数据强一致性，而非最终一致性
     * 实时数据需要等待设备响应
     * 1.修改数据库
     * 2.发送主题
     * 3.数据校验
     *
     * @param reqVO
     * @param request
     * @return
     * @throws Exception
     */
    @OperationAnnotation(content = "设置血压预警阈值")
    @PostMapping(value = "/setPressure")
    public BaseResponse setPressure(@RequestBody DeviceVO reqVO, HttpServletRequest request) throws Exception {
        SystemParam systemParam = null;
        String secretKey = commonService.getWebSalt(request);
        String language = MayiktJwtUtils.getLanguage(request, secretKey);
        Long userId = commonService.getUserId(request);
        if (!OptionalBean.ofNullable(reqVO.getImei()).isPresent()) {
            systemParam = systemParamByCacheService.getValues(DeviceConstant.DEVICE_ENUM, DeviceConstant.DEVICE_NO_IMEI_PREFIX + language);
            throw new DefinitException(systemParam.getCode(), systemParam.getMassage());
        }
        if (!OptionalBean.ofNullable(reqVO.getSbp()).isPresent()) {
            systemParam = systemParamByCacheService.getValues(DeviceConstant.DEVICE_ENUM, DeviceConstant.DEVICE_UN_SBP_PREFIX + language);
            throw new DefinitException(systemParam.getCode(), systemParam.getMassage());
        }
        if (!OptionalBean.ofNullable(reqVO.getDbp()).isPresent()) {
            systemParam = systemParamByCacheService.getValues(DeviceConstant.DEVICE_ENUM, DeviceConstant.DEVICE_UN_DBP_PREFIX + language);
            throw new DefinitException(systemParam.getCode(), systemParam.getMassage());
        }
        if (!OptionalBean.ofNullable(reqVO.getPressureInterval()).isPresent()) {
            systemParam = systemParamByCacheService.getValues(DeviceConstant.DEVICE_ENUM, DeviceConstant.DEVICE_UN_PRESSURE_INTERVAL_PREFIX + language);
            throw new DefinitException(systemParam.getCode(), systemParam.getMassage());
        }

        Device device = new Device();
        device.setImei(reqVO.getImei());
        device.setSbp(reqVO.getSbp());
        device.setDbp(reqVO.getDbp());
        device.setPressureInterval(reqVO.getPressureInterval());
        // 血压设置状态【0：修改成功 / 1：修改中】
        device.setPressureState(1);
        // 1.修改血压预警阈值
        deviceService.updateByImeiSelective(device);

        // 2.发送消息 【设置血压预警阈值】

        // 组装json
        JSONObject data = new JSONObject();
        data.put("code", EmqCode.pressureCode);
        data.put("imei", reqVO.getImei());
        data.put("sbp", reqVO.getSbp());
        data.put("dbp", reqVO.getDbp());
        data.put("interval", reqVO.getPressureInterval());
        // 推送设置血压预警阈值topic给设备
        publishService.sendToMqtt(data.toJSONString(), EmqCode.sendTopic + reqVO.getImei() + EmqCode.endTopic);
        systemParam = systemParamByCacheService.getValues(CrudConstant.CRUD_ENUM, CrudConstant.MODIFY_SUCCESS_PREFIX + language);
        return setResultSuccess(systemParam.getMassage());
    }


    /**
     * 查询血压预警阈值和血压上报时间间隔
     *
     * @param reqVO
     * @param request
     * @return
     * @throws Exception
     */
    @OperationAnnotation(content = "查询血压预警阈值和血压上报时间间隔")
    @PostMapping(value = "/getPressure")
    public BaseResponse getPressure(@RequestBody DeviceVO reqVO, HttpServletRequest request) throws Exception {
        SystemParam systemParam = null;
        String secretKey = commonService.getWebSalt(request);
        String language = MayiktJwtUtils.getLanguage(request, secretKey);
        Long userId = commonService.getUserId(request);
        if (!OptionalBean.ofNullable(reqVO.getImei()).isPresent()) {
            systemParam = systemParamByCacheService.getValues(DeviceConstant.DEVICE_ENUM, DeviceConstant.DEVICE_NO_IMEI_PREFIX + language);
            throw new DefinitException(systemParam.getCode(), systemParam.getMassage());
        }
        PressureDTO pressureDTO = deviceService.getPressureInfoByImei(reqVO.getImei());
        return setResultSuccess(pressureDTO);
    }


    /**
     * 定位数据上报时间间隔【分钟】
     * 产品经理要求此处数据强一致性，而非最终一致性
     * 实时数据需要等待设备响应
     * 1.修改数据库
     * 2.发送主题
     * 3.数据校验
     *
     * @param reqVO
     * @param request
     * @return
     * @throws Exception
     */
    @OperationAnnotation(content = "定位数据上报时间间隔【分钟】")
    @PostMapping(value = "/setLocationInterval")
    public BaseResponse setLocationInterval(@RequestBody DeviceVO reqVO, HttpServletRequest request) throws Exception {
        SystemParam systemParam = null;
        String secretKey = commonService.getWebSalt(request);
        String language = MayiktJwtUtils.getLanguage(request, secretKey);
        Long userId = commonService.getUserId(request);
        if (!OptionalBean.ofNullable(reqVO.getImei()).isPresent()) {
            systemParam = systemParamByCacheService.getValues(DeviceConstant.DEVICE_ENUM, DeviceConstant.DEVICE_NO_IMEI_PREFIX + language);
            throw new DefinitException(systemParam.getCode(), systemParam.getMassage());
        }
        if (!OptionalBean.ofNullable(reqVO.getLocationInterval()).isPresent()) {
            systemParam = systemParamByCacheService.getValues(DeviceConstant.DEVICE_ENUM, DeviceConstant.DEVICE_UN_POSITION_INTERVAL_PREFIX + language);
            throw new DefinitException(systemParam.getCode(), systemParam.getMassage());
        }

        Device device = new Device();
        device.setImei(reqVO.getImei());
        device.setLocationInterval(reqVO.getLocationInterval());
        // 定位数据上报时间间隔设置状态【0：修改成功 / 1：修改中】
        device.setLocationState(1);
        // 1.修改定位数据上报时间间隔
        deviceService.updateByImeiSelective(device);

        // 2.发送消息 【设置定位数据上报时间间隔】

        // 组装json
        JSONObject data = new JSONObject();
        data.put("code", EmqCode.locationIntervalCode);
        data.put("imei", reqVO.getImei());
        data.put("interval", reqVO.getLocationInterval());
        // 推送定位数据上报时间间隔topic给设备
        publishService.sendToMqtt(data.toJSONString(), EmqCode.sendTopic + reqVO.getImei() + EmqCode.endTopic);
        systemParam = systemParamByCacheService.getValues(CrudConstant.CRUD_ENUM, CrudConstant.MODIFY_SUCCESS_PREFIX + language);
        return setResultSuccess(systemParam.getMassage());
    }

    /**
     * 设备重启时间【分钟】
     * 产品经理要求此处数据强一致性，而非最终一致性
     * 实时数据需要等待设备响应
     * 1.修改数据库
     * 2.发送主题
     * 3.数据校验
     *
     * @param reqVO
     * @param request
     * @return
     * @throws Exception
     */
    @OperationAnnotation(content = "设备重启时间【分钟】")
    @PostMapping(value = "/setReloadInterval")
    public BaseResponse setReloadInterval(@RequestBody DeviceVO reqVO, HttpServletRequest request) throws Exception {
        SystemParam systemParam = null;
        String secretKey = commonService.getWebSalt(request);
        String language = MayiktJwtUtils.getLanguage(request, secretKey);
        Long userId = commonService.getUserId(request);
        if (!OptionalBean.ofNullable(reqVO.getImei()).isPresent()) {
            systemParam = systemParamByCacheService.getValues(DeviceConstant.DEVICE_ENUM, DeviceConstant.DEVICE_NO_IMEI_PREFIX + language);
            throw new DefinitException(systemParam.getCode(), systemParam.getMassage());
        }
        if (!OptionalBean.ofNullable(reqVO.getReloadInterval()).isPresent()) {
            systemParam = systemParamByCacheService.getValues(DeviceConstant.DEVICE_ENUM, DeviceConstant.DEVICE_UN_RELOAD_INTERVAL_PREFIX + language);
            throw new DefinitException(systemParam.getCode(), systemParam.getMassage());
        }

        Device device = new Device();
        device.setImei(reqVO.getImei());
        device.setReloadInterval(reqVO.getReloadInterval());
        // 设备重启时间设置状态【0：修改成功 / 1：修改中】
        device.setReloadState(1);
        // 1.修改设备重启时间
        deviceService.updateByImeiSelective(device);

        // 2.发送消息 【设置设备重启时间】

        // 组装json
        JSONObject data = new JSONObject();
        data.put("code", EmqCode.restartCode);
        data.put("imei", reqVO.getImei());
        data.put("interval", reqVO.getReloadInterval());
        // 推送设备重启时间topic给设备
        publishService.sendToMqtt(data.toJSONString(), EmqCode.sendTopic + reqVO.getImei() + EmqCode.endTopic);
        systemParam = systemParamByCacheService.getValues(CrudConstant.CRUD_ENUM, CrudConstant.MODIFY_SUCCESS_PREFIX + language);
        return setResultSuccess(systemParam.getMassage());
    }

    /**
     * 查询近电阈值
     *
     * @param reqVO
     * @param request
     * @return
     * @throws Exception
     */
    @OperationAnnotation(content = "查询近电阈值")
    @PostMapping(value = "/getVoltage")
    public BaseResponse getVoltage(@RequestBody DeviceVO reqVO, HttpServletRequest request) throws Exception {
        SystemParam systemParam = null;
        String secretKey = commonService.getWebSalt(request);
        String language = MayiktJwtUtils.getLanguage(request, secretKey);
        if (!OptionalBean.ofNullable(reqVO.getImei()).isPresent()) {
            systemParam = systemParamByCacheService.getValues(DeviceConstant.DEVICE_ENUM, DeviceConstant.DEVICE_NO_IMEI_PREFIX + language);
            throw new DefinitException(systemParam.getCode(), systemParam.getMassage());
        }
        VoltageDTO voltageDTO = deviceService.getVoltageInfoByImei(reqVO.getImei());
        return setResultSuccess(voltageDTO);
    }

    /**
     * 设置近电阈值
     * <p>
     * 产品经理要求此处数据强一致性，而非最终一致性
     * 实时数据需要等待设备响应
     * 1.修改数据库状态
     * 2.发送主题
     * 3.数据校验
     *
     * @param reqVO
     * @param request
     * @return
     * @throws Exception
     */
    @OperationAnnotation(content = "设置近电阈值")
    @PostMapping(value = "/setVoltage")
    public BaseResponse setVoltage(@RequestBody DeviceVO reqVO, HttpServletRequest request) throws Exception {
        SystemParam systemParam = null;
        String secretKey = commonService.getWebSalt(request);
        String language = MayiktJwtUtils.getLanguage(request, secretKey);
        Long userId = commonService.getUserId(request);
        if (!OptionalBean.ofNullable(reqVO.getImei()).isPresent()) {
            systemParam = systemParamByCacheService.getValues(DeviceConstant.DEVICE_ENUM, DeviceConstant.DEVICE_NO_IMEI_PREFIX + language);
            throw new DefinitException(systemParam.getCode(), systemParam.getMassage());
        }
        if (!OptionalBean.ofNullable(reqVO.getVoltage()).isPresent()) {
            systemParam = systemParamByCacheService.getValues(DeviceConstant.DEVICE_ENUM, DeviceConstant.DEVICE_NO_VOLTAGE_PREFIX + language);
            throw new DefinitException(systemParam.getCode(), systemParam.getMassage());
        }

        Device device = new Device();
        device.setImei(reqVO.getImei());
        device.setVoltage(reqVO.getVoltage());
        // 设备近电设置状态【0：修改成功 / 1：修改中】
        device.setVoltageState(1);
        // 1.修改心率预警阈值
        deviceService.updateByImeiSelective(device);

        // 2.发送消息 【设置心率预警阈值】

        // 组装json
        JSONObject data = new JSONObject();
        data.put("code", EmqCode.voltageCode);
        data.put("imei", reqVO.getImei());
        data.put("value", reqVO.getVoltage());
        // 推送设置心率预警阈值topic给设备
        publishService.sendToMqtt(data.toJSONString(), EmqCode.sendTopic + reqVO.getImei() + EmqCode.endTopic);
        systemParam = systemParamByCacheService.getValues(CrudConstant.CRUD_ENUM, CrudConstant.MODIFY_SUCCESS_PREFIX + language);
        return setResultSuccess(systemParam.getMassage());
    }

    /**
     * 查询设备检测有害气体间隔时间【分钟】 检测时间【秒】
     *
     * @param reqVO
     * @param request
     * @return
     * @throws Exception
     */
    @OperationAnnotation(content = "查询设备检测有害气体间隔时间【分钟】 检测时间【秒】")
    @PostMapping(value = "/getHarmfulGasInterval")
    public BaseResponse getHarmfulGasInterval(@RequestBody DeviceVO reqVO, HttpServletRequest request) throws Exception {
        SystemParam systemParam = null;
        String secretKey = commonService.getWebSalt(request);
        String language = MayiktJwtUtils.getLanguage(request, secretKey);
        Long userId = commonService.getUserId(request);
        if (!OptionalBean.ofNullable(reqVO.getImei()).isPresent()) {
            systemParam = systemParamByCacheService.getValues(DeviceConstant.DEVICE_ENUM, DeviceConstant.DEVICE_NO_IMEI_PREFIX + language);
            throw new DefinitException(systemParam.getCode(), systemParam.getMassage());
        }
        HarmfulGasDTO harmfulGasDTO = deviceService.getHarmfulGasInfoByImei(reqVO.getImei());
        return setResultSuccess(harmfulGasDTO);
    }

    /**
     * 设置检测有害气体间隔时间【分钟】 检测时间【秒】
     * 产品经理要求此处数据强一致性，而非最终一致性
     * 实时数据需要等待设备响应
     * 1.修改数据库
     * 2.发送主题
     * 3.数据校验
     *
     * @param reqVO
     * @param request
     * @return
     * @throws Exception
     */
    @OperationAnnotation(content = "设置检测有害气体间隔时间【分钟】 检测时间【秒】")
    @PostMapping(value = "/setHarmfulGasInterval")
    public BaseResponse setHarmfulGasInterval(@RequestBody DeviceVO reqVO, HttpServletRequest request) throws Exception {
        SystemParam systemParam = null;
        String secretKey = commonService.getWebSalt(request);
        String language = MayiktJwtUtils.getLanguage(request, secretKey);
        Long userId = commonService.getUserId(request);
        if (!OptionalBean.ofNullable(reqVO.getImei()).isPresent()) {
            systemParam = systemParamByCacheService.getValues(DeviceConstant.DEVICE_ENUM, DeviceConstant.DEVICE_NO_IMEI_PREFIX + language);
            throw new DefinitException(systemParam.getCode(), systemParam.getMassage());
        }
        if (!OptionalBean.ofNullable(reqVO.getHarmfulGasInterval()).isPresent()) {
            systemParam = systemParamByCacheService.getValues(DeviceConstant.DEVICE_ENUM, DeviceConstant.DEVICE_NO_INTERVAL_NULL_PREFIX + language);
            throw new DefinitException(systemParam.getCode(), systemParam.getMassage());
        }
        if (!OptionalBean.ofNullable(reqVO.getHarmfulGasDetectTime()).isPresent()) {
            systemParam = systemParamByCacheService.getValues(DeviceConstant.DEVICE_ENUM, DeviceConstant.DEVICE_NO_DETECT_TIME_NULL_PREFIX + language);
            throw new DefinitException(systemParam.getCode(), systemParam.getMassage());
        }

        Device device = new Device();
        device.setImei(reqVO.getImei());
        device.setHarmfulGasInterval(reqVO.getHarmfulGasInterval());
        device.setHarmfulGasDetectTime(reqVO.getHarmfulGasDetectTime());
        // 设备重启时间设置状态【0：修改成功 / 1：修改中】
        device.setHarmfulGasState(1);
        // 1.修改设备重启时间
        deviceService.updateByImeiSelective(device);

        // 2.发送消息 【设置设备有害气体】

        // 组装json
        JSONObject data = new JSONObject();
        data.put("code", EmqCode.harmfulGasCode);
        data.put("imei", reqVO.getImei());
        data.put("interval", reqVO.getHarmfulGasInterval()); //单位分钟，传0关闭间隔检测
        data.put("value", reqVO.getHarmfulGasDetectTime()); //检测时间，单位秒，到时间关闭检测
        // 推送设备重启时间topic给设备
        publishService.sendToMqtt(data.toJSONString(), EmqCode.sendTopic + reqVO.getImei() + EmqCode.endTopic);
        systemParam = systemParamByCacheService.getValues(CrudConstant.CRUD_ENUM, CrudConstant.MODIFY_SUCCESS_PREFIX + language);
        return setResultSuccess(systemParam.getMassage());
    }

    /**
     * 实时检测有害气体
     * 2.发送主题
     * 3.数据校验
     *
     * @param reqVO
     * @param request
     * @return
     * @throws Exception
     */
    @OperationAnnotation(content = "实时检测有害气体")
    @PostMapping(value = "/realTimeHarmfulGasInterval")
    public BaseResponse realTimeHarmfulGasInterval(@RequestBody DeviceVO reqVO, HttpServletRequest request) throws Exception {
        SystemParam systemParam = null;
        String secretKey = commonService.getWebSalt(request);
        String language = MayiktJwtUtils.getLanguage(request, secretKey);
        Long userId = commonService.getUserId(request);
        if (!OptionalBean.ofNullable(reqVO.getImei()).isPresent()) {
            systemParam = systemParamByCacheService.getValues(DeviceConstant.DEVICE_ENUM, DeviceConstant.DEVICE_NO_IMEI_PREFIX + language);
            throw new DefinitException(systemParam.getCode(), systemParam.getMassage());
        }
        if (!OptionalBean.ofNullable(reqVO.getValue()).isPresent()) {
            systemParam = systemParamByCacheService.getValues(DeviceConstant.DEVICE_ENUM, DeviceConstant.DEVICE_NO_VALUE_NULL_PREFIX + language);
            throw new DefinitException(systemParam.getCode(), systemParam.getMassage());
        }

        // 组装json
        JSONObject data = new JSONObject();
        data.put("code", EmqCode.realHarmfulGasCode);
        data.put("imei", reqVO.getImei());
        data.put("value", reqVO.getValue());
        // 推送设备检测结果topic给设备
        publishService.sendToMqtt(data.toJSONString(), EmqCode.sendTopic + reqVO.getImei() + EmqCode.endTopic);
        systemParam = systemParamByCacheService.getValues(CrudConstant.CRUD_ENUM, CrudConstant.MODIFY_SUCCESS_PREFIX + language);
        return setResultSuccess(systemParam.getMassage());
    }


    /**
     * 实时测量环境温度
     * 2.发送主题
     * 3.数据校验
     *
     * @param reqVO
     * @param request
     * @return
     * @throws Exception
     */
    @OperationAnnotation(content = "实时测量环境温度")
    @PostMapping(value = "/realTemperature")
    public BaseResponse realTemperature(@RequestBody DeviceVO reqVO, HttpServletRequest request) throws Exception {
        SystemParam systemParam = null;
        String secretKey = commonService.getWebSalt(request);
        String language = MayiktJwtUtils.getLanguage(request, secretKey);
        Long userId = commonService.getUserId(request);
        if (!OptionalBean.ofNullable(reqVO.getImei()).isPresent()) {
            systemParam = systemParamByCacheService.getValues(DeviceConstant.DEVICE_ENUM, DeviceConstant.DEVICE_NO_IMEI_PREFIX + language);
            throw new DefinitException(systemParam.getCode(), systemParam.getMassage());
        }

        // 组装json
        JSONObject data = new JSONObject();
        data.put("code", EmqCode.realTemperatureCode);
        data.put("imei", reqVO.getImei());
        // 推送设备检测结果topic给设备
        publishService.sendToMqtt(data.toJSONString(), EmqCode.sendTopic + reqVO.getImei() + EmqCode.endTopic);
        systemParam = systemParamByCacheService.getValues(CrudConstant.CRUD_ENUM, CrudConstant.MODIFY_SUCCESS_PREFIX + language);
        return setResultSuccess(systemParam.getMassage());
    }

    /**
     * 查询环境温度预警值+间隔
     *
     * @param reqVO
     * @param request
     * @return
     * @throws Exception
     */
    @OperationAnnotation(content = "查询环境温度预警值+间隔")
    @PostMapping(value = "/getTemperature")
    public BaseResponse getTemperature(@RequestBody DeviceVO reqVO, HttpServletRequest request) throws Exception {
        SystemParam systemParam = null;
        String secretKey = commonService.getWebSalt(request);
        String language = MayiktJwtUtils.getLanguage(request, secretKey);
        Long userId = commonService.getUserId(request);
        if (!OptionalBean.ofNullable(reqVO.getImei()).isPresent()) {
            systemParam = systemParamByCacheService.getValues(DeviceConstant.DEVICE_ENUM, DeviceConstant.DEVICE_NO_IMEI_PREFIX + language);
            throw new DefinitException(systemParam.getCode(), systemParam.getMassage());
        }
        OutTemperatureDTO outTemperatureDTO = deviceService.getTemperatureByImei(reqVO.getImei());
        return setResultSuccess(outTemperatureDTO);
    }

    /**
     * 设置环境温度预警值+间隔
     * 2.发送主题
     * 3.数据校验
     *
     * @param reqVO
     * @param request
     * @return
     * @throws Exception
     */
    @OperationAnnotation(content = "设置环境温度预警值+间隔")
    @PostMapping(value = "/setTemperature")
    public BaseResponse setTemperature(@RequestBody DeviceVO reqVO, HttpServletRequest request) throws Exception {
        SystemParam systemParam = null;
        String secretKey = commonService.getWebSalt(request);
        String language = MayiktJwtUtils.getLanguage(request, secretKey);
        Long userId = commonService.getUserId(request);
        if (!OptionalBean.ofNullable(reqVO.getImei()).isPresent()) {
            systemParam = systemParamByCacheService.getValues(DeviceConstant.DEVICE_ENUM, DeviceConstant.DEVICE_NO_IMEI_PREFIX + language);
            throw new DefinitException(systemParam.getCode(), systemParam.getMassage());
        }
        if (!OptionalBean.ofNullable(reqVO.getTemperatureInterval()).isPresent()) {
            systemParam = systemParamByCacheService.getValues(DeviceConstant.DEVICE_ENUM, DeviceConstant.DEVICE_NO_INTERVAL_NULL_PREFIX + language);
            throw new DefinitException(systemParam.getCode(), systemParam.getMassage());
        }
        if (!OptionalBean.ofNullable(reqVO.getTemperatureUpper()).isPresent()) {
            systemParam = systemParamByCacheService.getValues(DeviceConstant.DEVICE_ENUM, DeviceConstant.DEVICE_NO_UPPER_NULL_PREFIX + language);
            throw new DefinitException(systemParam.getCode(), systemParam.getMassage());
        }
        if (!OptionalBean.ofNullable(reqVO.getTemperatureLower()).isPresent()) {
            systemParam = systemParamByCacheService.getValues(DeviceConstant.DEVICE_ENUM, DeviceConstant.DEVICE_NO_LOWER_NULL_PREFIX + language);
            throw new DefinitException(systemParam.getCode(), systemParam.getMassage());
        }

        Device device = new Device();
        device.setImei(reqVO.getImei());
        device.setTemperatureUpper(reqVO.getTemperatureUpper());
        device.setTemperatureInterval(reqVO.getTemperatureInterval());
        device.setTemperatureLower(reqVO.getTemperatureLower());
        // 外温度间隔设置状态【0：修改成功 / 1：修改中】
        device.setTemperatureState(1);

        // 1.修改设备重启时间
        deviceService.updateByImeiSelective(device);

        // 2.发送消息 【设置设备有害气体】

        // 组装json
        JSONObject data = new JSONObject();
        data.put("code", EmqCode.temperatureCode);
        data.put("imei", reqVO.getImei());
        data.put("temperatureUpper", reqVO.getTemperatureUpper());
        data.put("temperatureLower", reqVO.getTemperatureLower());
        data.put("interval", reqVO.getTemperatureInterval());
        // 推送设备重启时间topic给设备
        publishService.sendToMqtt(data.toJSONString(), EmqCode.sendTopic + reqVO.getImei() + EmqCode.endTopic);
        systemParam = systemParamByCacheService.getValues(CrudConstant.CRUD_ENUM, CrudConstant.MODIFY_SUCCESS_PREFIX + language);
        return setResultSuccess(systemParam.getMassage());
    }


    /**
     * 实时测量海拔
     * 2.发送主题
     * 3.数据校验
     *
     * @param reqVO
     * @param request
     * @return
     * @throws Exception
     */
    @OperationAnnotation(content = "实时测量海拔")
    @PostMapping(value = "/realElevation")
    public BaseResponse realElevation(@RequestBody DeviceVO reqVO, HttpServletRequest request) throws Exception {
        SystemParam systemParam = null;
        String secretKey = commonService.getWebSalt(request);
        String language = MayiktJwtUtils.getLanguage(request, secretKey);
        Long userId = commonService.getUserId(request);
        if (!OptionalBean.ofNullable(reqVO.getImei()).isPresent()) {
            systemParam = systemParamByCacheService.getValues(DeviceConstant.DEVICE_ENUM, DeviceConstant.DEVICE_NO_IMEI_PREFIX + language);
            throw new DefinitException(systemParam.getCode(), systemParam.getMassage());
        }

        // 组装json
        JSONObject data = new JSONObject();
        data.put("code", EmqCode.realElevationCode);
        data.put("imei", reqVO.getImei());
        // 推送设备检测结果topic给设备
        publishService.sendToMqtt(data.toJSONString(), EmqCode.sendTopic + reqVO.getImei() + EmqCode.endTopic);
        systemParam = systemParamByCacheService.getValues(CrudConstant.CRUD_ENUM, CrudConstant.MODIFY_SUCCESS_PREFIX + language);
        return setResultSuccess(systemParam.getMassage());
    }

    /**
     * 查询海拔预警值+间隔
     *
     * @param reqVO
     * @param request
     * @return
     * @throws Exception
     */
    @OperationAnnotation(content = "查询海拔预警值+间隔")
    @PostMapping(value = "/getElevation")
    public BaseResponse getElevation(@RequestBody DeviceVO reqVO, HttpServletRequest request) throws Exception {
        SystemParam systemParam = null;
        String secretKey = commonService.getWebSalt(request);
        String language = MayiktJwtUtils.getLanguage(request, secretKey);
        Long userId = commonService.getUserId(request);
        if (!OptionalBean.ofNullable(reqVO.getImei()).isPresent()) {
            systemParam = systemParamByCacheService.getValues(DeviceConstant.DEVICE_ENUM, DeviceConstant.DEVICE_NO_IMEI_PREFIX + language);
            throw new DefinitException(systemParam.getCode(), systemParam.getMassage());
        }
        ElevationDTO elevationDTO = deviceService.getElevationByImei(reqVO.getImei());
        return setResultSuccess(elevationDTO);
    }

    /**
     * 设置海拔预警值+间隔
     * 2.发送主题
     * 3.数据校验
     *
     * @param reqVO
     * @param request
     * @return
     * @throws Exception
     */
    @OperationAnnotation(content = "设置海拔预警值+间隔")
    @PostMapping(value = "/setElevation")
    public BaseResponse setElevation(@RequestBody DeviceVO reqVO, HttpServletRequest request) throws Exception {
        SystemParam systemParam = null;
        String secretKey = commonService.getWebSalt(request);
        String language = MayiktJwtUtils.getLanguage(request, secretKey);
        Long userId = commonService.getUserId(request);
        if (!OptionalBean.ofNullable(reqVO.getImei()).isPresent()) {
            systemParam = systemParamByCacheService.getValues(DeviceConstant.DEVICE_ENUM, DeviceConstant.DEVICE_NO_IMEI_PREFIX + language);
            throw new DefinitException(systemParam.getCode(), systemParam.getMassage());
        }
        if (!OptionalBean.ofNullable(reqVO.getElevationInterval()).isPresent()) {
            systemParam = systemParamByCacheService.getValues(DeviceConstant.DEVICE_ENUM, DeviceConstant.DEVICE_NO_INTERVAL_NULL_PREFIX + language);
            throw new DefinitException(systemParam.getCode(), systemParam.getMassage());
        }
        if (!OptionalBean.ofNullable(reqVO.getElevationUpper()).isPresent()) {
            systemParam = systemParamByCacheService.getValues(DeviceConstant.DEVICE_ENUM, DeviceConstant.DEVICE_NO_UPPER_NULL_PREFIX + language);
            throw new DefinitException(systemParam.getCode(), systemParam.getMassage());
        }
        if (!OptionalBean.ofNullable(reqVO.getElevationLower()).isPresent()) {
            systemParam = systemParamByCacheService.getValues(DeviceConstant.DEVICE_ENUM, DeviceConstant.DEVICE_NO_LOWER_NULL_PREFIX + language);
            throw new DefinitException(systemParam.getCode(), systemParam.getMassage());
        }

        Device device = new Device();
        device.setImei(reqVO.getImei());
        device.setElevationUpper(reqVO.getElevationUpper());
        device.setElevationInterval(reqVO.getElevationInterval());
        device.setElevationLower(reqVO.getElevationLower());
        // 外温度间隔设置状态【0：修改成功 / 1：修改中】
        device.setElevationState(1);

        // 1.修改设备重启时间
        deviceService.updateByImeiSelective(device);

        // 2.发送消息 【设置设备有害气体】

        // 组装json
        JSONObject data = new JSONObject();
        data.put("code", EmqCode.elevationCode);
        data.put("imei", reqVO.getImei());
        data.put("elevationUpper", reqVO.getElevationUpper());
        data.put("elevationLower", reqVO.getElevationLower());
        data.put("interval", reqVO.getElevationInterval());
        // 推送设备重启时间topic给设备
        publishService.sendToMqtt(data.toJSONString(), EmqCode.sendTopic + reqVO.getImei() + EmqCode.endTopic);
        systemParam = systemParamByCacheService.getValues(CrudConstant.CRUD_ENUM, CrudConstant.MODIFY_SUCCESS_PREFIX + language);
        return setResultSuccess(systemParam.getMassage());
    }


    /**
     * 应用更新
     * 2.发送主题
     * 3.数据校验
     *
     * @param reqVO
     * @param request
     * @return
     * @throws Exception
     */
    @OperationAnnotation(content = "应用更新")
    @PostMapping(value = "/applyUpdate")
    public BaseResponse applyUpdate(@RequestBody DeviceVO reqVO, HttpServletRequest request) throws Exception {
        SystemParam systemParam = null;
        String secretKey = commonService.getWebSalt(request);
        String language = MayiktJwtUtils.getLanguage(request, secretKey);
        Long userId = commonService.getUserId(request);
        if (!OptionalBean.ofNullable(reqVO.getImei()).isPresent()) {
            systemParam = systemParamByCacheService.getValues(DeviceConstant.DEVICE_ENUM, DeviceConstant.DEVICE_NO_IMEI_PREFIX + language);
            throw new DefinitException(systemParam.getCode(), systemParam.getMassage());
        }

        // 组装json
        JSONObject data = new JSONObject();
        data.put("code", EmqCode.applyUpdateCode);
        data.put("imei", reqVO.getImei());
        // 推送设备检测结果topic给设备
        publishService.sendToMqtt(data.toJSONString(), EmqCode.sendTopic + reqVO.getImei() + EmqCode.endTopic);
        systemParam = systemParamByCacheService.getValues(CrudConstant.CRUD_ENUM, CrudConstant.MODIFY_SUCCESS_PREFIX + language);
        return setResultSuccess(systemParam.getMassage());
    }


    /**
     * 查询设备卡槽类型
     *
     * @param reqVO
     * @param request
     * @return
     * @throws Exception
     */
    @OperationAnnotation(content = "查询设备卡槽类型")
    @PostMapping(value = "/getSlotType")
    public BaseResponse getSlotType(@RequestBody DeviceVO reqVO, HttpServletRequest request) throws Exception {
        SystemParam systemParam = null;
        String secretKey = commonService.getWebSalt(request);
        String language = MayiktJwtUtils.getLanguage(request, secretKey);
        Long userId = commonService.getUserId(request);
        if (!OptionalBean.ofNullable(reqVO.getImei()).isPresent()) {
            systemParam = systemParamByCacheService.getValues(DeviceConstant.DEVICE_ENUM, DeviceConstant.DEVICE_NO_IMEI_PREFIX + language);
            throw new DefinitException(systemParam.getCode(), systemParam.getMassage());
        }
        SlotTypeDTO slotTypeDTO = deviceService.getSlotTypeByImei(reqVO.getImei());
        return setResultSuccess(slotTypeDTO);
    }


    /**
     * 设置卡槽类型
     * 1.记录
     * 2.发送主题
     * 3.数据校验
     *
     * @param reqVO
     * @param request
     * @return
     * @throws Exception
     */
    @OperationAnnotation(content = "设置卡槽类型")
    @PostMapping(value = "/setSlotType")
    public BaseResponse setSlotType(@RequestBody DeviceVO reqVO, HttpServletRequest request) throws Exception {
        SystemParam systemParam = null;
        String secretKey = commonService.getWebSalt(request);
        String language = MayiktJwtUtils.getLanguage(request, secretKey);
        Long userId = commonService.getUserId(request);
        if (!OptionalBean.ofNullable(reqVO.getImei()).isPresent()) {
            systemParam = systemParamByCacheService.getValues(DeviceConstant.DEVICE_ENUM, DeviceConstant.DEVICE_NO_IMEI_PREFIX + language);
            throw new DefinitException(systemParam.getCode(), systemParam.getMassage());
        }

        Device device = new Device();
        device.setImei(reqVO.getImei());
        // 卡槽类型 【0：卡槽1（插拔卡）/ 1：卡槽2（贴片卡）】
        //emp中进行修改
        //device.setSlotType(reqVO.getSlotType());
        //卡槽设置状态【0：修改成功 / 1:修改中】
        device.setSlotState(1);
        deviceService.updateByImeiSelective(device);

        // 组装json
        JSONObject data = new JSONObject();
        data.put("code", EmqCode.slotCode);
        data.put("imei", reqVO.getImei());
        data.put("value", reqVO.getSlotType());
        // 推送设备检测结果topic给设备
        publishService.sendToMqtt(data.toJSONString(), EmqCode.sendTopic + reqVO.getImei() + EmqCode.endTopic);
        systemParam = systemParamByCacheService.getValues(CrudConstant.CRUD_ENUM, CrudConstant.MODIFY_SUCCESS_PREFIX + language);
        return setResultSuccess(systemParam.getMassage());
    }

    /**
     * 查询佩戴欢迎语
     *
     * @param reqVO
     * @param request
     * @return
     * @throws Exception
     */
    @OperationAnnotation(content = "查询佩戴欢迎语")
    @PostMapping(value = "/getGreeting")
    public BaseResponse getGreeting(@RequestBody DeviceVO reqVO, HttpServletRequest request) throws Exception {
        SystemParam systemParam = null;
        String secretKey = commonService.getWebSalt(request);
        String language = MayiktJwtUtils.getLanguage(request, secretKey);
        Long userId = commonService.getUserId(request);
        if (!OptionalBean.ofNullable(reqVO.getImei()).isPresent()) {
            systemParam = systemParamByCacheService.getValues(DeviceConstant.DEVICE_ENUM, DeviceConstant.DEVICE_NO_IMEI_PREFIX + language);
            throw new DefinitException(systemParam.getCode(), systemParam.getMassage());
        }
        GreetingDTO greetingDTO = deviceService.getGreetingInfoByImei(reqVO.getImei());
        return setResultSuccess(greetingDTO);
    }

    /**
     * 设置帽子佩戴欢迎语
     * 1.记录
     * 2.发送主题
     * 3.数据校验
     *
     * @param reqVO
     * @param request
     * @return
     * @throws Exception
     */
    @OperationAnnotation(content = "设置帽子佩戴欢迎语")
    @PostMapping(value = "/setGreeting")
    public BaseResponse setGreeting(@RequestBody DeviceVO reqVO, HttpServletRequest request) throws Exception {
        SystemParam systemParam = null;
        String secretKey = commonService.getWebSalt(request);
        String language = MayiktJwtUtils.getLanguage(request, secretKey);
        Long userId = commonService.getUserId(request);
        if (!OptionalBean.ofNullable(reqVO.getImei()).isPresent()) {
            systemParam = systemParamByCacheService.getValues(DeviceConstant.DEVICE_ENUM, DeviceConstant.DEVICE_NO_IMEI_PREFIX + language);
            throw new DefinitException(systemParam.getCode(), systemParam.getMassage());
        }

        Device device = new Device();
        device.setImei(reqVO.getImei());
        device.setGreeting(reqVO.getGreeting());
        //欢迎语设置状态【0：修改成功 / 1:修改中】
        device.setGreetingState(1);
        deviceService.updateByImeiSelective(device);

        // 组装json
        JSONObject data = new JSONObject();
        data.put("code", EmqCode.greetingCode);
        data.put("imei", reqVO.getImei());
        data.put("info", reqVO.getGreeting());
        // 推送设备检测结果topic给设备
        publishService.sendToMqtt(data.toJSONString(), EmqCode.sendTopic + reqVO.getImei() + EmqCode.endTopic);
        systemParam = systemParamByCacheService.getValues(CrudConstant.CRUD_ENUM, CrudConstant.MODIFY_SUCCESS_PREFIX + language);
        return setResultSuccess(systemParam.getMassage());
    }
}
